package com.example.anew.journey.mynewjourney.firstchangelogic.frstChangeSecondTimeCardiacDetection.persenter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.example.anew.journey.mynewjourney.base.MyApplication;
import com.example.anew.journey.mynewjourney.bean.TakeDateBean;
import com.example.anew.journey.mynewjourney.config.RBConstants;
import com.example.anew.journey.mynewjourney.customview.MyVerticalScrollView;
import com.example.anew.journey.mynewjourney.firstchangelogic.firstChangeCardiacDetection.bean.ReSultBean;
import com.example.anew.journey.mynewjourney.firstchangelogic.firstChangeHistoricalRecordCardiacDetection.FirstChangeHistoricalRecordCardiacDetectionActvity;
import com.example.anew.journey.mynewjourney.firstchangelogic.frstChangeSecondTimeCardiacDetection.adpater.SecondTimeCardiacDetectionAdapter;
import com.example.anew.journey.mynewjourney.firstchangelogic.frstChangeSecondTimeCardiacDetection.bean.LastColumnsDateBean;
import com.example.anew.journey.mynewjourney.firstchangelogic.frstChangeSecondTimeCardiacDetection.model.FrstChangeSecondTimeCardiacDetectionModel;
import com.example.anew.journey.mynewjourney.firstchangelogic.frstChangeSecondTimeCardiacDetection.model.ISFrstChangeSecondTimeCardiacDetectionModel;
import com.example.anew.journey.mynewjourney.firstchangelogic.frstChangeSecondTimeCardiacDetection.view.ISFrstChangeSecondTimeCardiacDetectionView;
import com.example.anew.journey.mynewjourney.inteface.OnRecyclerViewItemClickListener;
import com.google.gson.Gson;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.Callback;

import java.util.List;

import cn.jesse.nativelogger.NLogger;
import okhttp3.Call;
import okhttp3.Response;

/**
 * 杭州惟君医疗科技有限公司14
 * Created by 20433 on 2018/3/22
 *
 * @desc 东风山河绿，春已不折柳，繁花看不尽，处处惹人留
 * @desc 佛祖镇楼，bug辟容易
 */

public class FrstChangeSecondTimeCardiacDetectionPersenter {
    private ISFrstChangeSecondTimeCardiacDetectionView mISFrstChangeSecondTimeCardiacDetectionView;
    private SecondTimeCardiacDetectionAdapter mSecondTimeCardiacDetectionAdapter;

    private ISFrstChangeSecondTimeCardiacDetectionModel mISFrstChangeSecondTimeCardiacDetectionModel;

    public FrstChangeSecondTimeCardiacDetectionPersenter(ISFrstChangeSecondTimeCardiacDetectionView iSFrstChangeSecondTimeCardiacDetectionView) {
        this.mISFrstChangeSecondTimeCardiacDetectionView = iSFrstChangeSecondTimeCardiacDetectionView;
        mISFrstChangeSecondTimeCardiacDetectionModel = new FrstChangeSecondTimeCardiacDetectionModel();
    }

    // 点击 小贴士
    public void onClickeTips() {
        mISFrstChangeSecondTimeCardiacDetectionView.toTipsPager();
    }

    //点击 开始测试
    public void onClickStartTest() {
        String str = mISFrstChangeSecondTimeCardiacDetectionView.getStartTest();
        if (str.equals("开始测试")) {
            mISFrstChangeSecondTimeCardiacDetectionView.toTest();
            //mISFrstChangeSecondTimeCardiacDetectionView.setStartTest("结束");
        } else {
            mISFrstChangeSecondTimeCardiacDetectionView.cancleTest();
           // mISFrstChangeSecondTimeCardiacDetectionView.setStartTest("开始测试");
        }
    }

    //上拉跳转
    public void PullUpJump(MyVerticalScrollView activityFirstChangeSecondTimeCariacDetectionMyVerticalScrollView) {
        activityFirstChangeSecondTimeCariacDetectionMyVerticalScrollView.setmScrollChangedListener(new MyVerticalScrollView.IScrollChangedListener() {
            @Override
            public void onScrolledToBottom() {
                mISFrstChangeSecondTimeCardiacDetectionView.toHistoricalRecord();
            }

            @Override
            public void onScrolledToTop() {

            }
        });

    }

    //初始化 RecyclerView
    public void initRecyclerView(final RecyclerView activityFirstChangeSecondTimeCariacDetectionRecyclerview, Context mcontxt) {
        LinearLayoutManager layoutManager = new LinearLayoutManager(mcontxt);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        activityFirstChangeSecondTimeCariacDetectionRecyclerview.setLayoutManager(layoutManager);
        mSecondTimeCardiacDetectionAdapter = new SecondTimeCardiacDetectionAdapter(mcontxt);
        activityFirstChangeSecondTimeCariacDetectionRecyclerview.setAdapter(mSecondTimeCardiacDetectionAdapter);
        mSecondTimeCardiacDetectionAdapter.setOnItemClickListener(new OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view) {
                int postion = activityFirstChangeSecondTimeCariacDetectionRecyclerview.getChildAdapterPosition(view);
                mISFrstChangeSecondTimeCardiacDetectionView.toHistoricalRecord();
            }

            @Override
            public void onItemFirstButtonClick(View v, int position) {

            }
        });
        loadLastColumnsDate(mcontxt);
    }


    private void loadLastColumnsDate(final Context mcontxt) {
        if (getUserID(mcontxt) == null) return;
        String url = RBConstants.SERVER_URL + RBConstants.LAST_COLUMNS;
        OkHttpUtils
                .post()
                .url(url)
                .addParams("cus_id", getUserID(mcontxt).getUser_id())
                .build()
                .execute(new Callback() {
                    @Override
                    public Object parseNetworkResponse(Response response, int id) throws Exception {
                        String NetworkResponseDate = response.body().string();
                        Gson gson = new Gson();
                        //  NLogger.e("TAG", "===11=========NetworkResponseDate=================" + NetworkResponseDate);
                        final LastColumnsDateBean lastColumnsDateBean = gson.fromJson(NetworkResponseDate, LastColumnsDateBean.class);
                        MyApplication.getmHandler().post(new Runnable() {
                            @Override
                            public void run() {
                                if (lastColumnsDateBean.errorCode == 200) {
                                    List<LastColumnsDateBean.DataBean> mbean = lastColumnsDateBean.data;
                                    if (mbean != null) {
                                        mSecondTimeCardiacDetectionAdapter.addItem(mbean);

                                    }
                                    String date =  mISFrstChangeSecondTimeCardiacDetectionModel.getTestDate(mcontxt);
                                    mISFrstChangeSecondTimeCardiacDetectionView.setMycardiacDetectionDate(date);

                                }
                            }
                        });
                        return null;
                    }

                    @Override
                    public void onError(Call call, Exception e, int id) {
                        MyApplication.getmHandler().post(new Runnable() {
                            @Override
                            public void run() {
                                String date =  mISFrstChangeSecondTimeCardiacDetectionModel.getTestDate(mcontxt);
                                mISFrstChangeSecondTimeCardiacDetectionView.setMycardiacDetectionDate(date);
                            }
                        });

                    }

                    @Override
                    public void onResponse(Object response, int id) {

                    }
                });
    }

    private TakeDateBean getUserID(Context mContext) {
        TakeDateBean bean = mISFrstChangeSecondTimeCardiacDetectionModel.TakeDate(mContext);
        if (bean == null) return null;
        return bean;
    }

    public void TestReSult(String str, final Context mContext) {
        String url = RBConstants.SERVER_URL + RBConstants.TEST_RESULT;
        OkHttpUtils
                .post()
                .url(url)
                .addParams("cus_id", getUserID(mContext).getUser_id())
                .addParams("result", str)
                .build()
                .execute(new Callback() {
                    @Override
                    public Object parseNetworkResponse(Response response, int id) throws Exception {
                        String NetworkResponseDate = response.body().string();
                        Gson gson = new Gson();
                         NLogger.e("TAG", "===main=========NetworkResponseDate=================" + NetworkResponseDate);
                        final ReSultBean reSultBean = gson.fromJson(NetworkResponseDate, ReSultBean.class);
                        MyApplication.getmHandler().post(new Runnable() {
                            @Override
                            public void run() {
                                if (reSultBean.errorCode == 200) {
                                    // todo 记录是否当天第一使用

                                    Intent intent =   new Intent(mContext,FirstChangeHistoricalRecordCardiacDetectionActvity.class);
                                    intent.putExtra("my_test_date",reSultBean.data.num);
                                    intent.putExtra("my_test_desc",reSultBean.data.result);
                                    mContext.startActivity(intent);
                                    mISFrstChangeSecondTimeCardiacDetectionView.findPager();
                                }
                            }
                        });
                        return null;
                    }

                    @Override
                    public void onError(Call call, Exception e, int id) {

                    }

                    @Override
                    public void onResponse(Object response, int id) {

                    }
                });
    }

    //连接搜索中
    public  void search(){
        mISFrstChangeSecondTimeCardiacDetectionView.showSearch();
    }

    public void cancleSearch(){
        mISFrstChangeSecondTimeCardiacDetectionView.cancleSearch();
    }
    public void failureSearch(){
        mISFrstChangeSecondTimeCardiacDetectionView.failureSearch();
    }
    public void connectSuccess(){
        mISFrstChangeSecondTimeCardiacDetectionView.connectSuccess();
    }

    // 连接断开
    public void Disconnect(){
        mISFrstChangeSecondTimeCardiacDetectionView.showDisconnect();
    }

    public void cancleDisconnect(){
        mISFrstChangeSecondTimeCardiacDetectionView.cancleDisconnect();
    }
}
